//
//  WKMnemonicsListCell.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/10.
//

#import "WKMnemonicsListCell.h"


@interface WKMnemonicsItemView : UIButton

@property(nonatomic,strong) UILabel *mnemonicsLbl;

@end


@implementation WKMnemonicsListModel

- (Class)cell {
    return WKMnemonicsListCell.class;
}

@end

@interface WKMnemonicsListCell ()

@property(nonatomic,strong) UIView *contentBoxView;

@property(nonatomic,strong) WKMnemonicsListModel *model;

@end

@implementation WKMnemonicsListCell

+ (CGSize)sizeForModel:(WKMnemonicsListModel *)model {
    
    return CGSizeMake(WKScreenWidth, 240.0f);
}

- (void)setupUI {
    [super setupUI];
    [self.contentView addSubview:self.contentBoxView];
}

- (void)refresh:(WKMnemonicsListModel *)model {
    [super refresh:model];
    self.model = model;
    
    [self.contentBoxView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if(model.items && model.items.count>0) {
        NSInteger i = 0;
        for (NSString *item in model.items) {
            [self.contentBoxView addSubview:[self newItemView:item index:i]];
            i++;
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentBoxView.lim_size = CGSizeMake(self.lim_width - 40.0f, self.lim_height);
    self.contentBoxView.lim_left = 20.0f;
    
    CGFloat space = 15.0f;
    NSInteger numOfRow = 3;
    NSInteger currRow = 1;
    NSInteger currCol = 1;
    CGFloat itemHeight = 40.0f;
    CGFloat itemWidth = (self.contentBoxView.lim_width - space*(numOfRow+1))/numOfRow;
    if(self.contentBoxView.subviews.count>0) {
        NSInteger i = 0;
        for (UIView *subview in self.contentBoxView.subviews) {
            subview.lim_size = CGSizeMake(itemWidth , itemHeight);
            if(i>0 && i%numOfRow == 0 ) {
                currRow++;
                currCol = 1;
            }
            subview.lim_top = (currRow-1 ) * itemHeight + currRow*space;
            subview.lim_left = (currCol - 1) * itemWidth + currCol*space;
            i++;
            currCol++;
            
            [subview layoutSubviews];
        }
    }
}

-(WKMnemonicsItemView*) newItemView:(NSString*)text index:(NSInteger)index{
    WKMnemonicsItemView *itemView = [[WKMnemonicsItemView alloc] init];
    itemView.mnemonicsLbl.text = text;
    itemView.backgroundColor = WKApp.shared.config.cellBackgroundColor;
    itemView.layer.masksToBounds = YES;
    itemView.layer.cornerRadius = 4.0f;
    itemView.tag = index;
    [itemView addTarget:self action:@selector(onItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    return itemView;
}

-(void) onItemPressed:(UIButton *)btn {
    if(!self.model.editOn) {
        return;
    }
    if(self.model.onItem) {
        self.model.onItem(btn.tag);
    }
}

- (UIView *)contentBoxView {
    if(!_contentBoxView) {
        _contentBoxView = [[UIView alloc] init];
        _contentBoxView.backgroundColor = WKApp.shared.config.backgroundColor;
        _contentBoxView.layer.cornerRadius = 8.0f;
        _contentBoxView.layer.masksToBounds = YES;
    }
    return _contentBoxView;
}
@end

@implementation WKMnemonicsItemView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void) setupUI {
    
    [self addSubview:self.mnemonicsLbl];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.mnemonicsLbl.lim_size = self.lim_size;
}

- (UILabel *)mnemonicsLbl {
    if(!_mnemonicsLbl) {
        _mnemonicsLbl = [[UILabel alloc] init];
        _mnemonicsLbl.textAlignment = NSTextAlignmentCenter;
        _mnemonicsLbl.font = [WKApp.shared.config appFontOfSizeMedium:15.0f];
    }
    return _mnemonicsLbl;
}

@end
